/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.doctor.entity.*;
import com.jims.master.modules.doctor.service.ClinicMasterService;
import com.jims.master.modules.doctor.service.PatVisitIndexService;
import com.jims.master.modules.doctor.service.PatVisitService;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 病人在院信息Controller
 *
 * @author 赵宁
 * @version 2017-03-27
 */
@Controller
@RequestMapping(value = "${adminPath}/doctor/patVisit")
public class PatVisitController extends BaseController {

    @Autowired
    private PatVisitService patVisitService;
    @Autowired
    private ClinicMasterService clinicMasterService;
    @Autowired
    private OrgDeptService orgDeptService;
    @Autowired
    private PatVisitIndexService patVisitIndexService;

    @ModelAttribute
    public PatVisit get(@RequestParam(required = false) String id) {
        PatVisit entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = patVisitService.get(id);
        }
        if (entity == null) {
            entity = new PatVisit();
        }
        return entity;
    }

    /**
     * 病人列表--住院
     *
     * @param patVisit
     * @param request
     * @param response
     * @param model
     * @return
     * @author 赵宁
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(PatVisit patVisit, HttpServletRequest request, HttpServletResponse response, Model model, String inFlag) {
        User user=UserUtils.getUser();
        OrgDept dept = orgDeptService.get(user.getOrgDept().getId());
        if(patVisit.getDeptStayedEt()!= null && StringUtils.isNotBlank(patVisit.getDeptStayedEt().getId())){
            user.setOrgDept(patVisit.getDeptStayedEt());
            UserUtils.updateCache(user);
        } else {
            patVisit.setDeptStayedEt(user.getOrgDept());
        }
        Page<PatVisit> page = patVisitService.findPage(new Page<PatVisit>(request, response), patVisit);
        List<User> result= UserUtils.getRoleUserList(dept.getId());
        model.addAttribute("users",result);
        model.addAttribute("page", page);
        return "modules/doctor/patientList";
    }

    /**
     * 入出院患者页面
     *
     * @return
     * @author 陈涛
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"patVisitInfo", ""})
    public String patVisitList(PatVisit patVisit,Model model) {
        if (StringUtils.isBlank(patVisit.getInFlag())){
            patVisit.setInFlag("0");
        }
        patVisit.updateOrgId();
        model.addAttribute("inFlag", patVisit.getInFlag());
        if (patVisit.getInFlag().equals("0")){
            patVisit.setWardCode(UserUtils.getUser().getOrgDept().getId());

            List<BaseDto> list = patVisitService.findPatientList(patVisit);

            model.addAttribute("list", list);
            return "modules/nurse/inPatVisitInfo";
        }else if (patVisit.getInFlag().equals("1")){

            patVisit.setWardDischargeTo(UserUtils.getUser().getOrgDept().getId());
            List<BaseDto> list = patVisitService.finOutPatientList(patVisit);
            model.addAttribute("list", list);
            return "modules/nurse/outPatVisitInfo";
        }

        return "modules/nurse/inPatVisitInfo";

    }

    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(PatVisit patVisit, Model model) {
        StringData data = new StringData();
        int i = patVisitService.save(patVisit);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存病人在院信息成功");
        } else {
            data.setCode("error");
            data.setData("保存病人在院信息失败");
        }
        return data;
    }

    /**
     * 住院登记保存
     *
     * @param patVisit
     * @param model
     * @return
     * @author 陈涛
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveInformation")
    @ResponseBody
    public StringData saveHospitalInformation(PatVisit patVisit, PatVisitIndex patVisitIndex, PatsInUnnursing patsInUnnursing, PatsInTransferring patsInTransferring, Model model, String clinicId) {
        StringData data  = patVisitService.saveInformation(patVisit, patVisitIndex, patsInUnnursing, patsInTransferring, clinicId);
        return data;
    }

    /**
     * 新生儿登记页面
     *
     * @return
     * @author 赵宁
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "newBorn")
    public String newBorn() {
        return "modules/nurse/newBornRegist";
    }

    /**
     * 新生儿住院登记页面
     *
     * @return
     * @author 赵宁
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "newBornInhos")
    public String newBornInhos() {
        return "modules/nurse/newBornInHos";
    }




	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(PatVisit patVisit) {
	    StringData data=new StringData();
		int i=patVisitService.delete(patVisit);
        if(i>0){
            data.setCode("success");
            data.setData("删除病人在院信息成功");
        } else {
            data.setCode("error");
            data.setData("删除病人在院信息失败");
        }
        return data;
    }

    /**
     * 床头卡片病人列表
     * @param patVisit
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "bedCardList")
    public String bedCardList(PatVisit patVisit, Model model, HttpServletRequest request, HttpServletResponse response) {
        patVisit.setWardCode(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
        patVisit.updateOrgId();
        Page<PatVisit> page =  patVisitService.findBedCardPage(new Page<PatVisit>(request, response), patVisit);
        model.addAttribute("patVisit", patVisit);
        model.addAttribute("page", page);
        return "modules/nurse/bedCardList";
    }

    /**
     * 床头卡片
     *
     * @param
     * @param model
     * @return
     * @author 陈涛
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "bedCard")
    public String getPatInfo(PatVisit patVisit, Model model, String visitNo) {
        if (visitNo == null) {
            visitNo = "-1";
        }
        patVisit = patVisitService.findPatInfoByNo(visitNo, UserUtils.getOrgId());
        model.addAttribute("patVisit", patVisit);
        return "modules/nurse/bedCard";
    }

    /**
     *卡片打印
     *
     * @param patVisit
     * @param model
     * @param visitNo
     * @return
     * @author 杨富达
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "bedsideCardPrint")
    @ResponseBody
    public StringData bedsideCardPrint(PatVisit patVisit, Model model) {
        StringData stringData = new StringData();
        String printPath = patVisitService.bedsideCardPrint(patVisit);
        stringData.setCode("success");
        stringData.setData(printPath);
        return stringData;
    }

    /**
     * 住院患者卡片打印
     * @param patVisit
     * @return
     * @author CTQ
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "normalPrint")
    @ResponseBody
    public StringData normalPrint(PatVisit patVisit) {
        StringData stringData = new StringData();
        String printPath = patVisitService.normalPrint(patVisit);
        stringData.setCode("success");
        stringData.setData(printPath);
        return stringData;
    }
    @RequiresPermissions("user")
    @RequestMapping(value = "updateUser")
    @ResponseBody
    public StringData updateUser(String visitNo,String doctorInCharge,String type){
        int num = 0;
        StringData stringData = new StringData();
        PatVisit patVisit = patVisitService.findPatInfoByNo(visitNo, UserUtils.getOrgId());
        if(patVisit != null){
            PatVisit patVisit1 = new PatVisit();
            patVisit1.setOrgId(UserUtils.getOrgId());
            patVisit1.setVisitNo(visitNo);
            if("1".equals(type)){
                patVisit1.setDoctorInCharge(doctorInCharge);
            } else if("2".equals(type)){
                patVisit1.setUpperDoctor(doctorInCharge);
            } else if("3".equals(type)){
                patVisit1.setDirectorDoctor(doctorInCharge);
            }
            num= patVisitService.updateUser(patVisit1);
        }
        if(num>0){
            stringData.setCode("success");
            stringData.setData("修改信息成功");
        } else {
            stringData.setCode("error");
            stringData.setData("修改信息失败");
        }
        return stringData;
    }


    /**
     * @Author chenxy
     * @desc 更新病人信息
     * @param patMasterIndex
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findById", ""})
    public String findById(String patientId,String visitId,HttpServletRequest request, HttpServletResponse response, Model model) {
        PatVisitIndex patVisitIndex = this.patVisitIndexService.getInpPateientinfo(visitId);
        model.addAttribute("patVisitIndex", patVisitIndex);
        return "modules/doctor/updatePatInfo";
    }

    /**
     * @Author ZhuQ
     * @desc 获取医嘱待退药的住院患者列表
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findByDrugIn", ""})
    public String findByDrugIn(PatVisit patVisit, HttpServletRequest request, HttpServletResponse response, Model model) {
        patVisit.updateOrgId();
        patVisit.setInFlag("1");
        patVisit.setDeptStayed(null != UserUtils.getUser().getOrgDept() ? UserUtils.getUser().getOrgDept().getId() : "");
        Page<PatVisit> page = patVisitService.findByDrugInPage(new Page<PatVisit>(request, response), patVisit);
        model.addAttribute("page", page);
        model.addAttribute("patVisit", patVisit);
        return "modules/drugOut/ordersDrugIn";
    }

    /**
     * @Author ZhuQ
     * @desc 获取医嘱已退药的住院患者列表
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findByDrugInRecord", ""})
    public String findByDrugInForm(PatVisit patVisit,HttpServletRequest request, HttpServletResponse response, Model model) {
        patVisit.updateOrgId();
        patVisit.setInFlag("2");
        patVisit.setDeptStayed(null != UserUtils.getUser().getOrgDept() ? UserUtils.getUser().getOrgDept().getId() : "");
        Page<PatVisit> page = patVisitService.findByDrugInPage(new Page<PatVisit>(request, response), patVisit);
        model.addAttribute("page", page);
        model.addAttribute("patVisit", patVisit);
        return "modules/drugOut/ordersDrugInRecord";
    }
}

















